var express = require('express');
var router = express.Router();
var async = require('async');

var MongoClient= require('mongodb').MongoClient;
<<<<<<< HEAD
var DB_CONN_STR = 'mongodb://localhost:27017/happy';
=======
var DB_CONN_STR = 'mongodb://10.31.155.79:27017/happy';
>>>>>>> 820adef492112074cbec43ee89a63f07647b667c


router.get('/',function(req,res){
    var pageNo = req.query.pageNo,
    pageNo = pageNo?pageNo:0,
    pageSize = 9,
    count = 0,
    totalPages = 0;
    MongoClient.connect(DB_CONN_STR,function(err,db){
        if(err){
        console.log(err);
        }else{
            console.log('connect success')
            var conn = db.collection('movielist');
            conn.find({}).toArray(function(err,results){
                if(err){
                console.log(err);
                return;
                }else{
                    console.log(results[0].movie_list[0].title);
                    console.log(pageNo);
                    count = results[0].movie_list.length;
                    totalPages = Math.ceil( count/pageSize);
                    res.render('movieindex',{
                        list:results[0],
                        pageNo:pageNo,
                        pageSize:pageSize,
                        count:count,
                        totalPages:totalPages-1
                    })
                    db.close();
                }
            })
        }
    })

});

router.get('/detail', function(req, res, next) {
    var id= req.query.id;
    var mid = req.query.mid;
    var data = {id:mid}

    var findData = function(db,callback){
            var conn_mlist = db.collection('movielist');
            var conn_mcomment = db.collection('moviecomments');

            async.parallel([
                function(callback){
                    conn_mlist.find({}).toArray(function(err,results){
                        if(err){
                            return;
                        }else{
                            callback(null,results);
                        }
                    })
                },
                function(callback){
                    conn_mcomment.find(data).sort({_id:-1}).toArray(function(err,results){
                        if(err){
                            return;
                        }else{
                            callback(null,results);
                        }
                    })
                }
            ],function(err,results){
                callback(results[0],results[1])
            })            
        }
        MongoClient.connect(DB_CONN_STR,function(err,db){
            if(err){
                return;
            }else{
                findData(db,function(movielist,comment){
                    var movieArr = movielist[0].movie_list[id];
<<<<<<< HEAD
                    console.log(movielist[0]);
=======
>>>>>>> 820adef492112074cbec43ee89a63f07647b667c
                    res.render('moviedetail',{
                        movieArr: movieArr,
                        comment:comment,
                        id:id
                    })
                    db.close();
                })
            }
        })
    
});


router.post('/submit',function(req,res,next){
    var id= req.query.id;
    var mid = req.query.mid;
<<<<<<< HEAD
    var username=req.session.username;
    console.log(username)
    if(username){
     console.log('');
        var content = req.body.content;
        var time = new Date().getTime();
       
=======
    
     if(username){
    //     console.log('');
        var content = req.body.content;
        var time = new Date().getTime();
        var username = '111';
>>>>>>> 820adef492112074cbec43ee89a63f07647b667c

        var inserData = function(db,callback){
            var conn = db.collection('moviecomments');

            var data = {id:mid,time:time,username:username,content:content};
            conn.insert(data,function(err,results){
                if(err) return;
                callback(results);
            })
        }
        MongoClient.connect(DB_CONN_STR,function(err,db){
            if(err){
                return;
            }else{
                inserData(db,function(results){
                   res.redirect('/movie/detail?id='+id+'&mid='+mid);
                    db.close();
                })
            }
        })
<<<<<<< HEAD
     }else{
         res.send('<script> alert("session过期，重新登录");location.href="/login"</script>');
      }
})

=======
      }else{
          res.send('<script> alert("session过期，重新登录");location.href="/login"</script>');
      }
})

router.get('/search',function(req,res,next){
    
    MongoClient.connect(DB_CONN_STR,function(err,db){
        if(err){
        console.log(err);
        }else{
            console.log('connect success')
            var conn = db.collection('movielist');
            conn.find({}).toArray(function(err,results){
                if(err){
                console.log(err);
                return;
                }else{
                    console.log(results[0].movie_list[0].title);
                    console.log(pageNo);
                    count = results[0].movie_list.length;
                    totalPages = Math.ceil( count/pageSize);
                    res.render('movieindex',{
                        list:results[0],
                        pageNo:pageNo,
                        pageSize:pageSize,
                        count:count,
                        totalPages:totalPages-1
                    })
                    db.close();
                }
            })
        }
    })
})

>>>>>>> 820adef492112074cbec43ee89a63f07647b667c
module.exports = router;